package com.iss.okair.db.mapper.sql;

import com.iss.common.StringTools;
import com.iss.okair.db.entity.AssetsCar;
import com.iss.okair.db.model.AssetsCarModel;
import jdk.nashorn.internal.objects.annotations.Where;
import org.apache.ibatis.jdbc.SQL;
/*
* 存放与assests_car表有关的生成动态sql造句方法的类
 * */
public class AssetsCarSQL {

    //生成条件查询的动态sql语句
    public String find(AssetsCarModel model){
    return new SQL(){{
        SELECT(" *,CONCAT(carBrand,'',carModel)");
        FROM("assets_car");
        if(model.getAssetsNameCn()!=null && !model.getAssetsNameCn().isEmpty()){
            WHERE("namecn like'%"+model.getAssetsNameCn()+"%'");
        }
        if(model.getAssetsCode()!=null && !model.getAssetsCode().isEmpty()){
            WHERE("assetscode like'%"+model.getAssetsCode()+"%'");
        }
        if(model.getBrandModel()!=null && !model.getBrandModel().isEmpty()){
            WHERE("brandmodel like'%"+model.getBrandModel()+"%'");
        }
        if(model.isAutoTrans()){
            WHERE("autotrans=1");
        }
        //无意义操作
//        else{
//            WHERE("autotrans=0");
//        }
        if(model.getPowerType()!=null && !"-1".equals(model.getPowerType())){
            WHERE("powerType=" + model.getPowerType());
        }
        if(model.getAssetsType2()!=null && !"-1".equals(model.getAssetsType2())){
            WHERE("assetsType=" + model.getAssetsType2());
        }
        if(model.getAssetsType1()!=null && !"-1".equals(model.getAssetsType1()) && "-1".equals(model.getAssetsType2())){
            WHERE("assetsType in(select id from category where parentId="+model.getAssetsType1()+ ")");
        }

    }}.toString();
    }

    //生成修改的动态sql语句的方法
    public String update(AssetsCar car){
        return new SQL(){{
            UPDATE("assets_car");
            if(car.getAssetstype()!=null && car.getAssetstype() != -1){
                SET("assetstype = #{assetstype}");
            }
            if(!StringTools.isNullOrEmpty(car.getNamecn())){
                SET("namecn = #{namecn}");
            }
            if(!StringTools.isNullOrEmpty(car.getEngineinfo())){
                SET("engineinfo = #{engineinfo}");
            }
            if(!StringTools.isNullOrEmpty(car.getCarbrand())){
                SET("carbrand = #{carbrand}");
            }
            if(!StringTools.isNullOrEmpty(car.getCarmodel())){
                SET("carmodel = #{carmodel}");
            }
            if(car.getPowertype()!=null && car.getPowertype() != -1){
                SET("powertype = #{powertype}");
            }
            if(car.getSitcount()!=null && car.getSitcount() != -1){
                SET("sitcount = #{sitcount}");
            }
            if(!StringTools.isNullOrEmpty(car.getComment())){
                SET("comment= #{comment}");
            }
            if(car.getAutotrans()!=null){
                SET("autotrans = #{autotrans}");
            }
            if(!StringTools.isNullOrEmpty(car.getCreateuser())){
                SET("createuser = #{createuser}");
            }
            WHERE("id=#{id}");
        }}.toString();
    }

}
